package com.example.statistics.ui.purchase

import android.os.Bundle
import android.view.View
import com.example.statistics.R
import com.example.statistics.bean.FuturesFeeBean
import com.example.statistics.common.BaseActivity
import com.example.statistics.ex.*
import com.example.statistics.net.UrlConfig
import com.example.statistics.utils.Constants
import com.example.statistics.utils.activityFinish
import com.jhj.httplibrary.model.HttpParams
import com.example.statistics.ex.afterTextChangedListener
import com.example.statistics.ex.textStringToNumber
import kotlinx.android.synthetic.main.activity_futures_fee.*
import java.util.Date


/**
 * 定金、尾款、税款费用
 */
class FuturesFeeActivity : BaseActivity() {

    private var futuresFeeId: Int = -1
    var dollar = 0.0

    override val title: String
        get() = intent.getStringExtra(Constants.TITLE) ?: "费用"


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_futures_fee)
        val purchaseProductId: Int
        val data = intent.getSerializableExtra(Constants.DATA) as FuturesFeeBean?
        val action = intent.getStringExtra(Constants.ACTION)

        dollar = intent.getDoubleExtra(Constants.DOLLAR, 0.0)
        initView()
        if (data != null) {//编辑或者是实际定金、尾款、税款
            if (action == Constants.ACTION_EDIT) { //如果是编辑
                futuresFeeId = data.ID
            }

            purchaseProductId = data.purchaseProductId
            tv_date.contentText = data.date.goTimeFormat()
            tv_dollar.contentText = data.dollar.toString()
            tv_exchange_rate.contentText = data.exchangeRate.toString()
            tv_rmb.contentText = data.rmb.toString()
            tv_wire_transfer_fee.contentText = data.wireTransferFee.toString()
            tv_handling_fee.contentText = data.handlingFee.toString()
            tv_total_fee.contentText = data.totalFee.toString()
        } else {
            //预付、预估
            purchaseProductId = intent.getIntExtra(Constants.PURCHASE_PRODUCT_ID, -1)
            tv_dollar.contentText = dollar.toString()
            tv_date.contentText = intent.getStringExtra(Constants.DATE).goTimeFormat()
            tv_handling_fee.contentText = intent.getFloatExtra(Constants.HANDING_FEE, 0f).toString()
            tv_wire_transfer_fee.contentText = intent.getFloatExtra(Constants.WIRE_TRANSFER_FEE, 0f).toString()
        }

        tv_dollar.labelText = if (title.contains("定金"))
            "定        金"
        else if (title.contains("保证金"))
            "保  证  金"
        else
            "尾        款"

        var accountId = 0
        tv_account.onContentClicked {

        }
        layout_commit.setOnCommitListener {

            if (tv_date.isBlank() || tv_dollar.isBlank() /*|| tv_exchange_rate.isBlank() || tv_rmb.isBlank() || tv_total_fee.isBlank()*/) {
                return@setOnCommitListener
            }
            val params = HttpParams()
            var url = UrlConfig.FUTURES_FEE_ADD
            if (futuresFeeId != -1) {
                params.put("ID" to futuresFeeId)
                url = UrlConfig.FUTURES_FEE_UPDATE
            }
            params.put(
                "purchaseProductId" to purchaseProductId,
                "feeType" to title,
                "date" to tv_date.contentText,
                "dollar" to tv_dollar.contentText,
                "exchangeRate" to tv_exchange_rate.contentText,
                "rmb" to tv_rmb.contentText,
                "wireTransferFee" to tv_wire_transfer_fee.contentText,
                "handlingFee" to tv_handling_fee.contentText,
                "totalFee" to tv_total_fee.contentText,
                "" to accountId
            )
            httpPost<FuturesFeeBean>(url, config = {
                httpParams = params
            }) {
                activityFinish()
            }
        }
    }

    private fun initView() {
        val margin = intent.getDoubleExtra(Constants.MARGIN, 0.0) //合计美元
        tv_margin.visibility = if (margin != 0.0) View.VISIBLE else View.GONE
        tv_margin.contentText = margin.toString()

        //汇率
        tv_exchange_rate.afterTextChangedListener { editable, _ ->
            val text = editable.toString()
            if (text.isNumber()) {
                val rmb = tv_dollar.textStringToNumber.mulEx(text.toDouble())
                tv_rmb.contentText = rmb.toString()
                totalFee()
            }
        }

        //人民币
        tv_rmb.afterTextChangedListener { editable, _ ->
            if (editable.toString().isNumber()) {
                val rate = tv_exchange_rate.textStringToNumber
                val rmb = editable.toString().toDouble()
                if (margin != 0.0) { //保证金
                    val marginRmb = margin.mulEx(rate)
                    tv_handling_fee.contentText = marginRmb.addEx(rmb).divEx(1000).toString()
                } else {
                    tv_handling_fee.contentText = rmb.divEx(1000).toString()
                }
                totalFee()
            }
        }

        tv_handling_fee.afterTextChangedListener { _, _ ->
            totalFee()
        }

        tv_wire_transfer_fee.afterTextChangedListener { editable, textView ->
            totalFee()
        }
    }

    private fun totalFee() {
        tv_total_fee.contentText = tv_rmb.textStringToNumber
            .addEx(tv_handling_fee.textStringToNumber)
            .addEx(tv_wire_transfer_fee.textStringToNumber).toString()
    }
}